package here.controller;

import here.model.Here;
import here.model.Usuario;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends AbstractCommand{
	
	public Login() {
	}

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try{
			Here here = Here.getInstance();
			String email = request.getParameter("email");
			String senha = request.getParameter("senha");
			
			for (Usuario u: here.getUsuarios()){
				if (u.getEmail().equals(email) &&
						u.getSenha().equals(senha)){
					RequestDispatcher view = request.getRequestDispatcher("/home.jsp");
					view.forward(request, response);
				}
			}
			
		}catch (Exception e){
			request.setAttribute("msgErro", e.getMessage());			
			request.getRequestDispatcher("/error2.jsp").forward(request, response);	
		}
		
	}

}
